Can't get min_position in twitter scraping correctly












1














I am trying to scrape twitter.
please enter search.twitter.com and put Comorbidity in search form.
I can get first page correctly, I can see when scrolling down for more tweets, the next page can be gotten from min_position param.
But when send request with next page, I can't get correct content.



Here is my some code.



headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'}

def start_requests(self):
yield Request(url=self.start_urls[0], callback=self.parse_search_page)

def parse_search_page(self, response):
keyword = 'Comorbidity'
search_url = self.search_url.format(keyword=keyword)
yield Request(url=search_url, callback=self.parse_twitter_page, headers=self.headers)

def parse_twitter_page(self, response):
next_page = None
if self.current_page == 0:
posts = response.xpath('//li[@data-item-type="tweet"]').extract()
min_position = re.search('data-min-position="(.*?)"', response.body)
if min_position:
min_position = min_position.group(1)
next_page = self.next_page_url.format(position=min_position.replace('cm+', 'cm%2B').replace('==', '%3D%3D'))
self.current_page = 1
else:
json_data = json.loads(response.body)
min_position = json_data.get('min_position')

if next_page:
yield scrapy.http.Request(
url=self.next_page_url,
callback=self.parse_twitter_page,
headers=self.headers,
)


How can I get correct min_position?










share|improve this question





























    1














    I am trying to scrape twitter.
    please enter search.twitter.com and put Comorbidity in search form.
    I can get first page correctly, I can see when scrolling down for more tweets, the next page can be gotten from min_position param.
    But when send request with next page, I can't get correct content.



    Here is my some code.



    headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'}

    def start_requests(self):
    yield Request(url=self.start_urls[0], callback=self.parse_search_page)

    def parse_search_page(self, response):
    keyword = 'Comorbidity'
    search_url = self.search_url.format(keyword=keyword)
    yield Request(url=search_url, callback=self.parse_twitter_page, headers=self.headers)

    def parse_twitter_page(self, response):
    next_page = None
    if self.current_page == 0:
    posts = response.xpath('//li[@data-item-type="tweet"]').extract()
    min_position = re.search('data-min-position="(.*?)"', response.body)
    if min_position:
    min_position = min_position.group(1)
    next_page = self.next_page_url.format(position=min_position.replace('cm+', 'cm%2B').replace('==', '%3D%3D'))
    self.current_page = 1
    else:
    json_data = json.loads(response.body)
    min_position = json_data.get('min_position')

    if next_page:
    yield scrapy.http.Request(
    url=self.next_page_url,
    callback=self.parse_twitter_page,
    headers=self.headers,
    )


    How can I get correct min_position?










    share|improve this question



























      1












      1








      1







      I am trying to scrape twitter.
      please enter search.twitter.com and put Comorbidity in search form.
      I can get first page correctly, I can see when scrolling down for more tweets, the next page can be gotten from min_position param.
      But when send request with next page, I can't get correct content.



      Here is my some code.



      headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'}

      def start_requests(self):
      yield Request(url=self.start_urls[0], callback=self.parse_search_page)

      def parse_search_page(self, response):
      keyword = 'Comorbidity'
      search_url = self.search_url.format(keyword=keyword)
      yield Request(url=search_url, callback=self.parse_twitter_page, headers=self.headers)

      def parse_twitter_page(self, response):
      next_page = None
      if self.current_page == 0:
      posts = response.xpath('//li[@data-item-type="tweet"]').extract()
      min_position = re.search('data-min-position="(.*?)"', response.body)
      if min_position:
      min_position = min_position.group(1)
      next_page = self.next_page_url.format(position=min_position.replace('cm+', 'cm%2B').replace('==', '%3D%3D'))
      self.current_page = 1
      else:
      json_data = json.loads(response.body)
      min_position = json_data.get('min_position')

      if next_page:
      yield scrapy.http.Request(
      url=self.next_page_url,
      callback=self.parse_twitter_page,
      headers=self.headers,
      )


      How can I get correct min_position?










      share|improve this question















      I am trying to scrape twitter.
      please enter search.twitter.com and put Comorbidity in search form.
      I can get first page correctly, I can see when scrolling down for more tweets, the next page can be gotten from min_position param.
      But when send request with next page, I can't get correct content.



      Here is my some code.



      headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'}

      def start_requests(self):
      yield Request(url=self.start_urls[0], callback=self.parse_search_page)

      def parse_search_page(self, response):
      keyword = 'Comorbidity'
      search_url = self.search_url.format(keyword=keyword)
      yield Request(url=search_url, callback=self.parse_twitter_page, headers=self.headers)

      def parse_twitter_page(self, response):
      next_page = None
      if self.current_page == 0:
      posts = response.xpath('//li[@data-item-type="tweet"]').extract()
      min_position = re.search('data-min-position="(.*?)"', response.body)
      if min_position:
      min_position = min_position.group(1)
      next_page = self.next_page_url.format(position=min_position.replace('cm+', 'cm%2B').replace('==', '%3D%3D'))
      self.current_page = 1
      else:
      json_data = json.loads(response.body)
      min_position = json_data.get('min_position')

      if next_page:
      yield scrapy.http.Request(
      url=self.next_page_url,
      callback=self.parse_twitter_page,
      headers=self.headers,
      )


      How can I get correct min_position?







      python scrapy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 19:25

























      asked Nov 11 at 19:03









      Softdev

      127




      127
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I think you had a wrong in parse_twitter_page method.



          if next_page:
          yield scrapy.http.Request(
          url=next_page,
          callback=self.parse_twitter_page,
          headers=self.headers,
          )


          It should not be self.next_page_url.
          I changed from self.next_page_url to next_page
          I hope this will works.






          share|improve this answer





















          • This worked for me. Thanks
            – Softdev
            Nov 12 at 12:11











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252155%2fcant-get-min-position-in-twitter-scraping-correctly%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I think you had a wrong in parse_twitter_page method.



          if next_page:
          yield scrapy.http.Request(
          url=next_page,
          callback=self.parse_twitter_page,
          headers=self.headers,
          )


          It should not be self.next_page_url.
          I changed from self.next_page_url to next_page
          I hope this will works.






          share|improve this answer





















          • This worked for me. Thanks
            – Softdev
            Nov 12 at 12:11
















          0














          I think you had a wrong in parse_twitter_page method.



          if next_page:
          yield scrapy.http.Request(
          url=next_page,
          callback=self.parse_twitter_page,
          headers=self.headers,
          )


          It should not be self.next_page_url.
          I changed from self.next_page_url to next_page
          I hope this will works.






          share|improve this answer





















          • This worked for me. Thanks
            – Softdev
            Nov 12 at 12:11














          0












          0








          0






          I think you had a wrong in parse_twitter_page method.



          if next_page:
          yield scrapy.http.Request(
          url=next_page,
          callback=self.parse_twitter_page,
          headers=self.headers,
          )


          It should not be self.next_page_url.
          I changed from self.next_page_url to next_page
          I hope this will works.






          share|improve this answer












          I think you had a wrong in parse_twitter_page method.



          if next_page:
          yield scrapy.http.Request(
          url=next_page,
          callback=self.parse_twitter_page,
          headers=self.headers,
          )


          It should not be self.next_page_url.
          I changed from self.next_page_url to next_page
          I hope this will works.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 12:08









          Dmytro L

          16




          16












          • This worked for me. Thanks
            – Softdev
            Nov 12 at 12:11


















          • This worked for me. Thanks
            – Softdev
            Nov 12 at 12:11
















          This worked for me. Thanks
          – Softdev
          Nov 12 at 12:11




          This worked for me. Thanks
          – Softdev
          Nov 12 at 12:11


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252155%2fcant-get-min-position-in-twitter-scraping-correctly%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          さくらももこ

          13 indicted, 8 arrested in Calif. drug cartel investigation