

{"id":2350,"date":"2016-10-27T12:00:47","date_gmt":"2016-10-27T16:00:47","guid":{"rendered":"https:\/\/sites.temple.edu\/tudsc\/?p=2350"},"modified":"2020-04-22T15:40:35","modified_gmt":"2020-04-22T19:40:35","slug":"scraping-for-studying-online-political-discussion-part-2-python","status":"publish","type":"post","link":"https:\/\/sites.temple.edu\/tudsc\/2016\/10\/27\/scraping-for-studying-online-political-discussion-part-2-python\/","title":{"rendered":"Scraping for Studying Online Political Discussion Part 2: Python"},"content":{"rendered":"<p>By Luling Huang<\/p>\n<p><!--more--><\/p>\n<p><span style=\"text-decoration: underline\"><a href=\"https:\/\/sites.temple.edu\/tudsc\/2016\/09\/20\/scraping-for-studying-online-political-discussion\/\">In my last post<\/a><\/span>, I started to collect my data with the Chrome extension Web Scraper. I have included an expanded\u00a0<a href=\"https:\/\/sites.temple.edu\/tudsc\/files\/2016\/10\/DemonstrationofWebScraper.pdf\"><span style=\"text-decoration: underline\">demonstration of how I used Web Scraper\u00a0here\u00a0(opens in new window)<\/span><\/a>.<\/p>\n<p>The extension can get things done. However, I didn&#8217;t have enough control over\u00a0what I want in my data set. Here are some of the troubles:<\/p>\n<p>(1) I don&#8217;t have much control on what the data structure looks like.<\/p>\n<p>(2) If some information is not displayed on the web, the visual-cue-based selectors of Web Scraper might not work. For example, I want to use &#8216;post ids&#8217; to label each post, but the web page does not display the ids, which means that it is difficult to &#8220;click to select&#8221; with Web Scraper.<\/p>\n<p>(3) If a tag has a child, and there are texts both under and outside the child, it is difficult to &#8220;click to select&#8221; only those texts outside the child with Web Scraper. Unfortunately, this is how the post contents are structured in html on the forum. This is crucial because I need to separate each post&#8217;s original content from its quoted content (if there is any).<\/p>\n<p>(4) I can&#8217;t find a way to identify myself to the web server of the forum. This is important considering that identifying oneself is one of the scraping courtesies.<\/p>\n<p>To deal with these issues, I turn to python. Inspired by <span style=\"text-decoration: underline\"><a href=\"https:\/\/sites.temple.edu\/tudsc\/2016\/09\/01\/web-scraping-with-python\/\">Andrea Siotto&#8217;s (2016) post on scraping Wikipedia<\/a><\/span>, I thought it was a good time to start learning python and to do some scrapings as exercise. I managed to write a scraper in python to get the data I wanted. It is doable in a few weeks once you have basic understanding of the python language, the data structures in python, the Beautiful Soup python library, and basics of regular expression. The rest is to google for troubleshooting.<\/p>\n<p>I&#8217;m sharing my codes below and it might not be the most elegant way to do the work. If you have any ideas to improve the scraper, feel free to leave a comment. Also, I hope some parts of the codes would be useful if you are scraping similar websites, or come across similar troubles.<\/p>\n<p>Due to the space limit, I am not going into details of what each line of code does. I will explain the general logic of my scraping based on the discussion of nested dictionary in python, and how I dealt with the specific troubles I mentioned above.<\/p>\n<p><strong>General logic:<\/strong><\/p>\n<p>(1) Store the scraped data in python as a nested dictionary.<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff;overflow: auto;width: auto;border: solid gray;border-width: .1em .1em .1em .8em;padding: .2em .6em\">\n<pre style=\"margin: 0;line-height: 125%\">test<span style=\"color: #333333\">=<\/span>{<span style=\"background-color: #fff0f0\">'A'<\/span>:{<span style=\"background-color: #fff0f0\">'a'<\/span>:<span style=\"background-color: #fff0f0\">'5'<\/span>,<span style=\"background-color: #fff0f0\">'b'<\/span>:<span style=\"background-color: #fff0f0\">'4'<\/span>,<span style=\"background-color: #fff0f0\">'c'<\/span>:<span style=\"background-color: #fff0f0\">'5'<\/span>,<span style=\"background-color: #fff0f0\">'d'<\/span>:<span style=\"background-color: #fff0f0\">'7'<\/span>,<span style=\"background-color: #fff0f0\">'e'<\/span>:<span style=\"background-color: #fff0f0\">'8'<\/span>},\r\n    <span style=\"background-color: #fff0f0\">'B'<\/span>:{<span style=\"background-color: #fff0f0\">'a'<\/span>:<span style=\"background-color: #fff0f0\">'3'<\/span>,<span style=\"background-color: #fff0f0\">'b'<\/span>:<span style=\"background-color: #fff0f0\">'8'<\/span>,<span style=\"background-color: #fff0f0\">'c'<\/span>:<span style=\"background-color: #fff0f0\">'8'<\/span>,<span style=\"background-color: #fff0f0\">'d'<\/span>:<span style=\"background-color: #fff0f0\">'10'<\/span>,<span style=\"background-color: #fff0f0\">'e'<\/span>:<span style=\"background-color: #fff0f0\">'1'<\/span>}}\r\n<\/pre>\n<\/div>\n<p>A basic dictionary in python is composed of multiple key-value pairs. Values of keys can simply be texts and numbers. The values can also be dictionaries. In this later case, inner dictionaries are nested under an outer dictionary. A simple two-layer nested dictionary is shown above. &#8220;test&#8221; is the outer dictionary, where &#8216;A&#8217; and &#8216;B&#8217; are the outer keys, &#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;, and &#8216;e&#8217; are the inner keys, and the numbers (as strings) are the values of inner keys. To make it more interpretable, &#8216;A&#8217; and &#8216;B&#8217; can be labels of two observations, &#8216;a&#8217; to &#8216;e&#8217; can be five attributes of observations, and the numbers can be measures of the attributes. If we write the dictionary into a csv file, it looks like this:<\/p>\n<p><a href=\"https:\/\/sites.temple.edu\/tudsc\/files\/2016\/10\/Screen-Shot-2016-10-17-at-1.45.18-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2410\" src=\"https:\/\/sites.temple.edu\/tudsc\/files\/2016\/10\/Screen-Shot-2016-10-17-at-1.45.18-PM.png\" alt=\"nested dictionary as csv\" width=\"185\" height=\"92\" \/><\/a><\/p>\n<p>Note that we can reproduce the same csv file with the following lists:<\/p>\n<div style=\"background: #ffffff;overflow: auto;width: auto;border: solid gray;border-width: .1em .1em .1em .8em;padding: .2em .6em\">\n<pre style=\"margin: 0;line-height: 125%\">cases<span style=\"color: #333333\">=<\/span>[<span style=\"background-color: #fff0f0\">'A'<\/span>,<span style=\"background-color: #fff0f0\">'B'<\/span>]\r\na<span style=\"color: #333333\">=<\/span>[<span style=\"background-color: #fff0f0\">'5'<\/span>,<span style=\"background-color: #fff0f0\">'3'<\/span>]\r\nb<span style=\"color: #333333\">=<\/span>[<span style=\"background-color: #fff0f0\">'4'<\/span>,<span style=\"background-color: #fff0f0\">'8'<\/span>]\r\nc<span style=\"color: #333333\">=<\/span>[<span style=\"background-color: #fff0f0\">'5'<\/span>,<span style=\"background-color: #fff0f0\">'8'<\/span>]\r\nd<span style=\"color: #333333\">=<\/span>[<span style=\"background-color: #fff0f0\">'7'<\/span>,<span style=\"background-color: #fff0f0\">'10'<\/span>]\r\ne<span style=\"color: #333333\">=<\/span>[<span style=\"background-color: #fff0f0\">'8'<\/span>,<span style=\"background-color: #fff0f0\">'1'<\/span>]\r\n<\/pre>\n<\/div>\n<p>Why is a single nested dictionary preferred to a group of lists? Two reasons: (I) Lists are mutable in python. A list can be easily changed without knowing how changes are made. If, for example, the items in lists are somehow reordered, the resulting csv will present the data differently from the ways measures are originally recorded. If it is a nested dictionary, it is safer because the key-value pair is fixed after being recorded; (II) It is more efficient to search through a nested dictionary than a group of lists.\u00a0Frances Zlotnick (2014) provides <span style=\"text-decoration: underline\">an excellent explanation<\/span> on this matter.<\/p>\n<p>In my case, the unit of analysis is post. Each post has a unique id number and 17 other attributes I&#8217;m interested in. Therefore, I assign the &#8216;post ids&#8217; to be the outer keys and the 18 attributes as 18 inner keys.<\/p>\n<p>(2) A big trunk of the codes below (from line 24 to line 125) is doing one thing: creating a list for each attribute containing information that will be used later as values of inner keys in the nested dictionary. This task includes locating the appropriate html tags, looping through the tags and retrieving information, and append each piece of retrieved information to the corresponding lists.<\/p>\n<p>For example, lines 32 to 38 do the following: 1. create an empty list called &#8220;authorlist;&#8221; 2. locate all the<\/p>\n<div>\n<p>tags of the class &#8220;popupmenu memberaction&#8221; (because post authors can be found under these tags by examining the web&#8217;s source code), which returns a list containing these tags; 3. loop through this list: for each<\/p>\n<div>\n<p>tag, find the <a> tags of the class &#8220;username online popupctrl&#8221; and the class &#8220;username offline popupctrl.&#8221; We need two classes of <\/a><a> because the forum makes a distinction between post authors who are online and offline at the moment; 4. extract the author name and append it to &#8220;authorlist.&#8221; After looping, each author name becomes an item in &#8220;authorlist.&#8221;<\/a><\/p>\n<p>(3) The next trunk of code (lines 132 to 157) loops through all the lists together, assigns post ids as outer keys, assigns attribute names as inner keys, and pairs inner keys with the values from the 18 lists just created.<\/p>\n<p>(4) The nested dictionary is now created but it&#8217;s only for one page at the post level (there are 10 posts maximum on each page under a thread). Some threads have hundreds of posts. Also, at the thread level, there are also multiple pages (20 threads maximum on each page). In total, there are more than 1,200 threads as of 10\/24\/2016.<\/p>\n<p>Therefore, the scraper needs to perform the following three tasks to deal with pagination:<\/p>\n<p>A. Make a thread-to-post connection. After the scraper collects thread urls at the thread-level page, it will &#8220;click and open&#8221; each of those urls and parse it (see lines 11 to 22);<\/p>\n<p>B. Loop through all pages at the post level under each thread until no &#8220;next page&#8221; button can be found (see the &#8220;while&#8221; loop statement at line 20, and lines 161-167);<\/p>\n<p>C. When it finishes scraping at one page at the thread level, loop through following\u00a0pages at the thread level until no &#8220;next page&#8221; button can be found (see the &#8220;while&#8221; loop statement at line 10, and lines 169-175).<\/p>\n<p>(5) Writing the nested dictionary to csv (lines 177-213). The output looks like this:<\/p>\n<p><a href=\"https:\/\/sites.temple.edu\/tudsc\/files\/2016\/10\/Screen-Shot-2016-10-17-at-6.14.31-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2419\" src=\"https:\/\/sites.temple.edu\/tudsc\/files\/2016\/10\/Screen-Shot-2016-10-17-at-6.14.31-PM.png\" alt=\"screen-shot-2016-10-17-at-6-14-31-pm\" width=\"1139\" height=\"375\" srcset=\"https:\/\/sites.temple.edu\/tudsc\/files\/2016\/10\/Screen-Shot-2016-10-17-at-6.14.31-PM.png 1139w, https:\/\/sites.temple.edu\/tudsc\/files\/2016\/10\/Screen-Shot-2016-10-17-at-6.14.31-PM-300x99.png 300w, https:\/\/sites.temple.edu\/tudsc\/files\/2016\/10\/Screen-Shot-2016-10-17-at-6.14.31-PM-768x253.png 768w, https:\/\/sites.temple.edu\/tudsc\/files\/2016\/10\/Screen-Shot-2016-10-17-at-6.14.31-PM-1024x337.png 1024w, https:\/\/sites.temple.edu\/tudsc\/files\/2016\/10\/Screen-Shot-2016-10-17-at-6.14.31-PM-700x230.png 700w, https:\/\/sites.temple.edu\/tudsc\/files\/2016\/10\/Screen-Shot-2016-10-17-at-6.14.31-PM-232x76.png 232w, https:\/\/sites.temple.edu\/tudsc\/files\/2016\/10\/Screen-Shot-2016-10-17-at-6.14.31-PM-464x153.png 464w, https:\/\/sites.temple.edu\/tudsc\/files\/2016\/10\/Screen-Shot-2016-10-17-at-6.14.31-PM-624x205.png 624w\" sizes=\"auto, (max-width: 1139px) 100vw, 1139px\" \/><\/a><\/p>\n<p>In this output file, I have already reordered the cases based on thread ids. With additional help of the attribute &#8220;post position&#8221; (a smaller number means an earlier reply under a thread), it is possible for me to reconstruct the temporal sequence of posts under each thread.<\/p>\n<p>The last time I ran the scraper, I collected more than 43,900 posts. It took my personal computer about 4 hours and 40 minutes to run.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8;overflow: auto;width: auto;border: solid gray;border-width: .1em .1em .1em .8em;padding: .2em .6em\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0;line-height: 125%\">  1\r\n  2\r\n  3\r\n  4\r\n  5\r\n  6\r\n  7\r\n  8\r\n  9\r\n 10\r\n 11\r\n 12\r\n 13\r\n 14\r\n 15\r\n 16\r\n 17\r\n 18\r\n 19\r\n 20\r\n 21\r\n 22\r\n 23\r\n 24\r\n 25\r\n 26\r\n 27\r\n 28\r\n 29\r\n 30\r\n 31\r\n 32\r\n 33\r\n 34\r\n 35\r\n 36\r\n 37\r\n 38\r\n 39\r\n 40\r\n 41\r\n 42\r\n 43\r\n 44\r\n 45\r\n 46\r\n 47\r\n 48\r\n 49\r\n 50\r\n 51\r\n 52\r\n 53\r\n 54\r\n 55\r\n 56\r\n 57\r\n 58\r\n 59\r\n 60\r\n 61\r\n 62\r\n 63\r\n 64\r\n 65\r\n 66\r\n 67\r\n 68\r\n 69\r\n 70\r\n 71\r\n 72\r\n 73\r\n 74\r\n 75\r\n 76\r\n 77\r\n 78\r\n 79\r\n 80\r\n 81\r\n 82\r\n 83\r\n 84\r\n 85\r\n 86\r\n 87\r\n 88\r\n 89\r\n 90\r\n 91\r\n 92\r\n 93\r\n 94\r\n 95\r\n 96\r\n 97\r\n 98\r\n 99\r\n100\r\n101\r\n102\r\n103\r\n104\r\n105\r\n106\r\n107\r\n108\r\n109\r\n110\r\n111\r\n112\r\n113\r\n114\r\n115\r\n116\r\n117\r\n118\r\n119\r\n120\r\n121\r\n122\r\n123\r\n124\r\n125\r\n126\r\n127\r\n128\r\n129\r\n130\r\n131\r\n132\r\n133\r\n134\r\n135\r\n136\r\n137\r\n138\r\n139\r\n140\r\n141\r\n142\r\n143\r\n144\r\n145\r\n146\r\n147\r\n148\r\n149\r\n150\r\n151\r\n152\r\n153\r\n154\r\n155\r\n156\r\n157\r\n158\r\n159\r\n160\r\n161\r\n162\r\n163\r\n164\r\n165\r\n166\r\n167\r\n168\r\n169\r\n170\r\n171\r\n172\r\n173\r\n174\r\n175\r\n176\r\n177\r\n178\r\n179\r\n180\r\n181\r\n182\r\n183\r\n184\r\n185\r\n186\r\n187\r\n188\r\n189\r\n190\r\n191\r\n192\r\n193\r\n194\r\n195\r\n196\r\n197\r\n198\r\n199\r\n200\r\n201\r\n202\r\n203\r\n204\r\n205\r\n206\r\n207\r\n208\r\n209\r\n210\r\n211\r\n212\r\n213\r\n214\r\n215<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0;line-height: 125%\"><span style=\"color: #008000;font-weight: bold\">import<\/span> <span style=\"color: #0000ff;font-weight: bold\">time<\/span>\r\n<span style=\"color: #008000;font-weight: bold\">import<\/span> <span style=\"color: #0000ff;font-weight: bold\">re<\/span>\r\n<span style=\"color: #008000;font-weight: bold\">import<\/span> <span style=\"color: #0000ff;font-weight: bold\">urllib2<\/span>\r\n<span style=\"color: #008000;font-weight: bold\">from<\/span> <span style=\"color: #0000ff;font-weight: bold\">bs4<\/span> <span style=\"color: #008000;font-weight: bold\">import<\/span> BeautifulSoup\r\n\r\nnext_threadpage<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'http:\/\/www.debatepolitics.com\/2016-us-presidential-election\/'<\/span>\r\nheaders<span style=\"color: #666666\">=<\/span>{<span style=\"color: #ba2121\">'User-Agent'<\/span>:<span style=\"color: #ba2121\">'Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_10_3);Luling Huang\/Temple University\/luling.huang@temple.edu'<\/span>}\r\npostdatabig<span style=\"color: #666666\">=<\/span>{}\r\n\r\n<span style=\"color: #008000;font-weight: bold\">while<\/span> <span style=\"color: #008000\">True<\/span>:\r\n    request <span style=\"color: #666666\">=<\/span> urllib2<span style=\"color: #666666\">.<\/span>Request(next_threadpage, <span style=\"color: #008000\">None<\/span>, headers)\r\n    soup2 <span style=\"color: #666666\">=<\/span> BeautifulSoup(urllib2<span style=\"color: #666666\">.<\/span>urlopen(request)<span style=\"color: #666666\">.<\/span>read(),<span style=\"color: #ba2121\">'lxml'<\/span>)\r\n    time<span style=\"color: #666666\">.<\/span>sleep(<span style=\"color: #666666\">2<\/span>)\r\n    tagh3<span style=\"color: #666666\">=<\/span>soup2<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'h3'<\/span>,class_<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'threadtitle'<\/span>)\r\n    threadurls<span style=\"color: #666666\">=<\/span>[div<span style=\"color: #666666\">.<\/span>a[<span style=\"color: #ba2121\">'href'<\/span>] <span style=\"color: #008000;font-weight: bold\">for<\/span> div <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> tagh3]\r\n\r\n    <span style=\"color: #008000;font-weight: bold\">for<\/span> threadurl <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> threadurls:    \r\n        threadurl<span style=\"color: #666666\">=<\/span>threadurl<span style=\"color: #666666\">.<\/span>split(<span style=\"color: #ba2121\">'?'<\/span>)[<span style=\"color: #666666\">0<\/span>]\r\n        next_postpage<span style=\"color: #666666\">=<\/span>threadurl\r\n        <span style=\"color: #008000;font-weight: bold\">while<\/span> <span style=\"color: #008000\">True<\/span>:\r\n            request2 <span style=\"color: #666666\">=<\/span> urllib2<span style=\"color: #666666\">.<\/span>Request(next_postpage, <span style=\"color: #008000\">None<\/span>, headers)\r\n            soup<span style=\"color: #666666\">=<\/span>BeautifulSoup(urllib2<span style=\"color: #666666\">.<\/span>urlopen(request2)<span style=\"color: #666666\">.<\/span>read(),<span style=\"color: #ba2121\">'lxml'<\/span>)\r\n            time<span style=\"color: #666666\">.<\/span>sleep(<span style=\"color: #666666\">2<\/span>)\r\n            threadtitles<span style=\"color: #666666\">=<\/span>soup<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'span'<\/span>,class_<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'threadtitle'<\/span>)\r\n            <span style=\"color: #008000;font-weight: bold\">for<\/span> element <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> threadtitles:\r\n                threadtitle<span style=\"color: #666666\">=<\/span>element<span style=\"color: #666666\">.<\/span>a<span style=\"color: #666666\">.<\/span>get_text()\r\n                postlink<span style=\"color: #666666\">=<\/span>element<span style=\"color: #666666\">.<\/span>a[<span style=\"color: #ba2121\">'href'<\/span>]<span style=\"color: #666666\">.<\/span>split(<span style=\"color: #ba2121\">'?'<\/span>)[<span style=\"color: #666666\">0<\/span>]\r\n                n<span style=\"color: #666666\">=<\/span>re<span style=\"color: #666666\">.<\/span>search(<span style=\"color: #ba2121\">r'(?, element<span style=\"color: #666666\">.<\/span>a[<span style=\"color: #ba2121\">'href'<\/span>])\r\n                <span style=\"color: #008000;font-weight: bold\">if<\/span> n <span style=\"color: #aa22ff;font-weight: bold\">is<\/span> <span style=\"color: #aa22ff;font-weight: bold\">not<\/span> <span style=\"color: #008000\">None<\/span>:\r\n                    threadid<span style=\"color: #666666\">=<\/span>n<span style=\"color: #666666\">.<\/span>group()\r\n        \r\n            authorlist<span style=\"color: #666666\">=<\/span>[]\r\n            divtagauthors<span style=\"color: #666666\">=<\/span>soup<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'div'<\/span>,class_<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">\"popupmenu memberaction\"<\/span>)\r\n            <span style=\"color: #008000;font-weight: bold\">for<\/span> element <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> divtagauthors:\r\n                <span style=\"color: #008000;font-weight: bold\">for<\/span> postauthoron <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> element<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'a'<\/span>,class_<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'username online popupctrl'<\/span>):\r\n                    authorlist<span style=\"color: #666666\">.<\/span>append(postauthoron<span style=\"color: #666666\">.<\/span>get_text())\r\n                <span style=\"color: #008000;font-weight: bold\">for<\/span> postauthoroff <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> element<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'a'<\/span>,class_<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'username offline popupctrl'<\/span>):\r\n                    authorlist<span style=\"color: #666666\">.<\/span>append(postauthoroff<span style=\"color: #666666\">.<\/span>get_text())\r\n\r\n            postpositions<span style=\"color: #666666\">=<\/span>[]\r\n            postcounters<span style=\"color: #666666\">=<\/span>soup<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'a'<\/span>,class_<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'postcounter'<\/span>)\r\n            <span style=\"color: #008000;font-weight: bold\">for<\/span> postauthor, postcount <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> <span style=\"color: #008000\">zip<\/span>(authorlist,postcounters):\r\n                <span style=\"color: #008000;font-weight: bold\">if<\/span> postcount<span style=\"color: #666666\">.<\/span>get_text()<span style=\"color: #666666\">==<\/span><span style=\"color: #ba2121\">'#1'<\/span>:\r\n                    threadauthor<span style=\"color: #666666\">=<\/span>postauthor\r\n            <span style=\"color: #008000;font-weight: bold\">for<\/span> element <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> postcounters:\r\n                postposition<span style=\"color: #666666\">=<\/span>element<span style=\"color: #666666\">.<\/span>get_text()<span style=\"color: #666666\">.<\/span>split(<span style=\"color: #ba2121\">'#'<\/span>)[<span style=\"color: #666666\">1<\/span>]\r\n                postpositions<span style=\"color: #666666\">.<\/span>append(<span style=\"color: #008000\">int<\/span>(postposition))\r\n\r\n            spandates<span style=\"color: #666666\">=<\/span>soup<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'span'<\/span>,class_<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'date'<\/span>)\r\n            \r\n            userinfos<span style=\"color: #666666\">=<\/span>soup<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'dl'<\/span>,<span style=\"color: #ba2121\">'userinfo_extra'<\/span>)\r\n            joindates<span style=\"color: #666666\">=<\/span>[]\r\n            locations<span style=\"color: #666666\">=<\/span>[]\r\n            lastseens<span style=\"color: #666666\">=<\/span>[]\r\n            genders<span style=\"color: #666666\">=<\/span>[]\r\n            leans<span style=\"color: #666666\">=<\/span>[]\r\n            postnumbers<span style=\"color: #666666\">=<\/span>[]\r\n            <span style=\"color: #008000;font-weight: bold\">for<\/span> element <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> userinfos:\r\n                <span style=\"color: #008000;font-weight: bold\">if<\/span> element<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'dt'<\/span>,string<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'Join Date'<\/span>) <span style=\"color: #aa22ff;font-weight: bold\">is<\/span> <span style=\"color: #aa22ff;font-weight: bold\">not<\/span> <span style=\"color: #008000\">None<\/span>:\r\n                    founddt1<span style=\"color: #666666\">=<\/span>element<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'dt'<\/span>,string<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'Join Date'<\/span>)\r\n                    joindates<span style=\"color: #666666\">.<\/span>append(founddt1<span style=\"color: #666666\">.<\/span>next_sibling<span style=\"color: #666666\">.<\/span>next_sibling<span style=\"color: #666666\">.<\/span>string)\r\n                <span style=\"color: #008000;font-weight: bold\">else<\/span>:\r\n                    joindates<span style=\"color: #666666\">.<\/span>append(<span style=\"color: #ba2121\">'NA'<\/span>)\r\n                <span style=\"color: #008000;font-weight: bold\">if<\/span> element<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'dt'<\/span>,string<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'Location'<\/span>) <span style=\"color: #aa22ff;font-weight: bold\">is<\/span> <span style=\"color: #aa22ff;font-weight: bold\">not<\/span> <span style=\"color: #008000\">None<\/span>:\r\n                    founddt2<span style=\"color: #666666\">=<\/span>element<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'dt'<\/span>,string<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'Location'<\/span>)\r\n                    locations<span style=\"color: #666666\">.<\/span>append(founddt2<span style=\"color: #666666\">.<\/span>next_sibling<span style=\"color: #666666\">.<\/span>next_sibling<span style=\"color: #666666\">.<\/span>string)\r\n                <span style=\"color: #008000;font-weight: bold\">else<\/span>:\r\n                    locations<span style=\"color: #666666\">.<\/span>append(<span style=\"color: #ba2121\">'NA'<\/span>)\r\n                <span style=\"color: #008000;font-weight: bold\">if<\/span> element<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'dt'<\/span>,string<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'Last Seen'<\/span>) <span style=\"color: #aa22ff;font-weight: bold\">is<\/span> <span style=\"color: #aa22ff;font-weight: bold\">not<\/span> <span style=\"color: #008000\">None<\/span>:\r\n                    founddt3<span style=\"color: #666666\">=<\/span>element<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'dt'<\/span>,string<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'Last Seen'<\/span>)\r\n                    lastseens<span style=\"color: #666666\">.<\/span>append(founddt3<span style=\"color: #666666\">.<\/span>next_sibling<span style=\"color: #666666\">.<\/span>string)\r\n                <span style=\"color: #008000;font-weight: bold\">else<\/span>:\r\n                    lastseens<span style=\"color: #666666\">.<\/span>append(<span style=\"color: #ba2121\">'NA'<\/span>)\r\n                <span style=\"color: #008000;font-weight: bold\">if<\/span> element<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'dt'<\/span>,string<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'Gender'<\/span>) <span style=\"color: #aa22ff;font-weight: bold\">is<\/span> <span style=\"color: #aa22ff;font-weight: bold\">not<\/span> <span style=\"color: #008000\">None<\/span>:\r\n                    founddt4<span style=\"color: #666666\">=<\/span>element<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'dt'<\/span>,string<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'Gender'<\/span>)\r\n                    <span style=\"color: #008000;font-weight: bold\">if<\/span> founddt4<span style=\"color: #666666\">.<\/span>next_sibling<span style=\"color: #666666\">.<\/span>next_sibling<span style=\"color: #666666\">.<\/span>find(src<span style=\"color: #666666\">=<\/span>re<span style=\"color: #666666\">.<\/span>compile(<span style=\"color: #ba2121\">'Female'<\/span>)) <span style=\"color: #aa22ff;font-weight: bold\">is<\/span> <span style=\"color: #aa22ff;font-weight: bold\">not<\/span> <span style=\"color: #008000\">None<\/span>:\r\n                        genders<span style=\"color: #666666\">.<\/span>append(<span style=\"color: #ba2121\">'Female'<\/span>)\r\n                    <span style=\"color: #008000;font-weight: bold\">elif<\/span> founddt4<span style=\"color: #666666\">.<\/span>next_sibling<span style=\"color: #666666\">.<\/span>next_sibling<span style=\"color: #666666\">.<\/span>find(src<span style=\"color: #666666\">=<\/span>re<span style=\"color: #666666\">.<\/span>compile(<span style=\"color: #ba2121\">'Male'<\/span>)) <span style=\"color: #aa22ff;font-weight: bold\">is<\/span> <span style=\"color: #aa22ff;font-weight: bold\">not<\/span> <span style=\"color: #008000\">None<\/span>:\r\n                        genders<span style=\"color: #666666\">.<\/span>append(<span style=\"color: #ba2121\">'Male'<\/span>)\r\n                    <span style=\"color: #008000;font-weight: bold\">else<\/span>:\r\n                        genders<span style=\"color: #666666\">.<\/span>append(<span style=\"color: #ba2121\">'NA'<\/span>)\r\n                <span style=\"color: #008000;font-weight: bold\">else<\/span>:\r\n                    genders<span style=\"color: #666666\">.<\/span>append(<span style=\"color: #ba2121\">'NA'<\/span>)\r\n                <span style=\"color: #008000;font-weight: bold\">if<\/span> element<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'dt'<\/span>,string<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'Lean'<\/span>) <span style=\"color: #aa22ff;font-weight: bold\">is<\/span> <span style=\"color: #aa22ff;font-weight: bold\">not<\/span> <span style=\"color: #008000\">None<\/span>:\r\n                    founddt5<span style=\"color: #666666\">=<\/span>element<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'dt'<\/span>,string<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'Lean'<\/span>)\r\n                    leans<span style=\"color: #666666\">.<\/span>append(founddt5<span style=\"color: #666666\">.<\/span>next_sibling<span style=\"color: #666666\">.<\/span>next_sibling<span style=\"color: #666666\">.<\/span>string)\r\n                <span style=\"color: #008000;font-weight: bold\">else<\/span>:\r\n                    leans<span style=\"color: #666666\">.<\/span>append(<span style=\"color: #ba2121\">'NA'<\/span>)\r\n                <span style=\"color: #008000;font-weight: bold\">if<\/span> element<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'dt'<\/span>,string<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'Posts'<\/span>) <span style=\"color: #aa22ff;font-weight: bold\">is<\/span> <span style=\"color: #aa22ff;font-weight: bold\">not<\/span> <span style=\"color: #008000\">None<\/span>:\r\n                    founddt6<span style=\"color: #666666\">=<\/span>element<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'dt'<\/span>,string<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'Posts'<\/span>)\r\n                    postnumbers<span style=\"color: #666666\">.<\/span>append(founddt6<span style=\"color: #666666\">.<\/span>next_sibling<span style=\"color: #666666\">.<\/span>next_sibling<span style=\"color: #666666\">.<\/span>string)\r\n                <span style=\"color: #008000;font-weight: bold\">else<\/span>:\r\n                    postnumbers<span style=\"color: #666666\">.<\/span>append(<span style=\"color: #ba2121\">'NA'<\/span>)\r\n            \r\n            addressees<span style=\"color: #666666\">=<\/span>[]\r\n            quoteauthors<span style=\"color: #666666\">=<\/span>[]\r\n            quotecontents<span style=\"color: #666666\">=<\/span>[]\r\n            originalcontents<span style=\"color: #666666\">=<\/span>[]\r\n            postcontents<span style=\"color: #666666\">=<\/span>soup<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'blockquote'<\/span>,<span style=\"color: #ba2121\">'postcontent restore '<\/span>)\r\n            <span style=\"color: #008000;font-weight: bold\">for<\/span> postcontent,postcount <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> <span style=\"color: #008000\">zip<\/span>(postcontents,postcounters):\r\n                <span style=\"color: #008000;font-weight: bold\">if<\/span> postcount<span style=\"color: #666666\">.<\/span>string<span style=\"color: #666666\">==<\/span><span style=\"color: #ba2121\">'#1'<\/span>:\r\n                    addressees<span style=\"color: #666666\">.<\/span>append(<span style=\"color: #ba2121\">'group'<\/span>)\r\n                <span style=\"color: #008000;font-weight: bold\">elif<\/span> postcontent<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'div'<\/span>,<span style=\"color: #ba2121\">'bbcode_container'<\/span>) <span style=\"color: #aa22ff;font-weight: bold\">is<\/span> <span style=\"color: #008000\">None<\/span>:\r\n                    addressees<span style=\"color: #666666\">.<\/span>append(threadauthor)\r\n                <span style=\"color: #008000;font-weight: bold\">else<\/span>:\r\n                    <span style=\"color: #008000;font-weight: bold\">for<\/span> quoteauthor <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> postcontent<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'strong'<\/span>):\r\n                        addressees<span style=\"color: #666666\">.<\/span>append(quoteauthor<span style=\"color: #666666\">.<\/span>string)\r\n                <span style=\"color: #008000;font-weight: bold\">if<\/span> postcontent<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'strong'<\/span>) <span style=\"color: #aa22ff;font-weight: bold\">is<\/span> <span style=\"color: #aa22ff;font-weight: bold\">not<\/span> <span style=\"color: #008000\">None<\/span>:\r\n                    <span style=\"color: #008000;font-weight: bold\">for<\/span> quoteauthor <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> postcontent<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'strong'<\/span>):\r\n                        quoteauthors<span style=\"color: #666666\">.<\/span>append(quoteauthor<span style=\"color: #666666\">.<\/span>string)\r\n                <span style=\"color: #008000;font-weight: bold\">else<\/span>:\r\n                    quoteauthors<span style=\"color: #666666\">.<\/span>append(<span style=\"color: #ba2121\">'NA'<\/span>)\r\n                <span style=\"color: #008000;font-weight: bold\">if<\/span> postcontent<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'div'<\/span>,<span style=\"color: #ba2121\">'bbcode_container'<\/span>) <span style=\"color: #aa22ff;font-weight: bold\">is<\/span> <span style=\"color: #aa22ff;font-weight: bold\">not<\/span> <span style=\"color: #008000\">None<\/span>:\r\n                    <span style=\"color: #008000;font-weight: bold\">if<\/span> postcontent<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'div'<\/span>,<span style=\"color: #ba2121\">'message'<\/span>) <span style=\"color: #aa22ff;font-weight: bold\">is<\/span> <span style=\"color: #aa22ff;font-weight: bold\">not<\/span> <span style=\"color: #008000\">None<\/span>:\r\n                        <span style=\"color: #008000;font-weight: bold\">for<\/span> quotecontent <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> postcontent<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'div'<\/span>,<span style=\"color: #ba2121\">'message'<\/span>):\r\n                            quotecontents<span style=\"color: #666666\">.<\/span>append(quotecontent<span style=\"color: #666666\">.<\/span>get_text(<span style=\"color: #ba2121\">' '<\/span>, strip<span style=\"color: #666666\">=<\/span><span style=\"color: #008000\">True<\/span>))\r\n                    <span style=\"color: #008000;font-weight: bold\">else<\/span>:\r\n                        <span style=\"color: #008000;font-weight: bold\">for<\/span> quotecontent <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> postcontent<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'div'<\/span>,<span style=\"color: #ba2121\">'quote_container'<\/span>):\r\n                            quotecontents<span style=\"color: #666666\">.<\/span>append(quotecontent<span style=\"color: #666666\">.<\/span>get_text(<span style=\"color: #ba2121\">' '<\/span>, strip<span style=\"color: #666666\">=<\/span><span style=\"color: #008000\">True<\/span>))\r\n                <span style=\"color: #008000;font-weight: bold\">else<\/span>:\r\n                    quotecontents<span style=\"color: #666666\">.<\/span>append(<span style=\"color: #ba2121\">'NA'<\/span>)\r\n            <span style=\"color: #008000;font-weight: bold\">for<\/span> element <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> postcontents:\r\n                <span style=\"color: #008000;font-weight: bold\">if<\/span> element<span style=\"color: #666666\">.<\/span>find(<span style=\"color: #ba2121\">'div'<\/span>) <span style=\"color: #aa22ff;font-weight: bold\">is<\/span> <span style=\"color: #aa22ff;font-weight: bold\">not<\/span> <span style=\"color: #008000\">None<\/span>:\r\n                    element<span style=\"color: #666666\">.<\/span>div<span style=\"color: #666666\">.<\/span>decompose()\r\n                originalcontents<span style=\"color: #666666\">.<\/span>append(element<span style=\"color: #666666\">.<\/span>get_text(<span style=\"color: #ba2121\">' '<\/span>, strip<span style=\"color: #666666\">=<\/span><span style=\"color: #008000\">True<\/span>))\r\n            \r\n            postdata<span style=\"color: #666666\">=<\/span>{}\r\n\r\n            toplitag<span style=\"color: #666666\">=<\/span>soup<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'li'<\/span>,class_<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">'postbitlegacy postbitim postcontainer old'<\/span>)\r\n\r\n   \r\n            <span style=\"color: #008000;font-weight: bold\">for<\/span> (litagid,postposition,spandate,postauthor,joindate,location,\r\n                lastseen,gender,lean,postnumber,addressee,quoteauthor,\r\n                quotecontent,originalcontent) <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> <span style=\"color: #008000\">zip<\/span>(toplitag,postpositions,\r\n                spandates,authorlist,joindates,locations,lastseens,genders,\r\n                leans,postnumbers,addressees,quoteauthors,quotecontents,originalcontents):\r\n                m<span style=\"color: #666666\">=<\/span>re<span style=\"color: #666666\">.<\/span>search(<span style=\"color: #ba2121\">r'(?, litagid[<span style=\"color: #ba2121\">'id'<\/span>])\r\n                <span style=\"color: #008000;font-weight: bold\">if<\/span> m <span style=\"color: #aa22ff;font-weight: bold\">is<\/span> <span style=\"color: #aa22ff;font-weight: bold\">not<\/span> <span style=\"color: #008000\">None<\/span>:\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())]<span style=\"color: #666666\">=<\/span>{}\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'PostPosition'<\/span>]<span style=\"color: #666666\">=<\/span>postposition\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'PostDateAndTime'<\/span>]<span style=\"color: #666666\">=<\/span>spandate<span style=\"color: #666666\">.<\/span>get_text()\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'PostAuthor'<\/span>]<span style=\"color: #666666\">=<\/span>postauthor\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'JoinDate'<\/span>]<span style=\"color: #666666\">=<\/span>joindate\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'Location'<\/span>]<span style=\"color: #666666\">=<\/span>location\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'LastSeen'<\/span>]<span style=\"color: #666666\">=<\/span>lastseen\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'Gender'<\/span>]<span style=\"color: #666666\">=<\/span>gender\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'IdeologicalLeaning'<\/span>]<span style=\"color: #666666\">=<\/span>lean\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'NumberOfPosts'<\/span>]<span style=\"color: #666666\">=<\/span>postnumber\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'Addressee'<\/span>]<span style=\"color: #666666\">=<\/span>addressee\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'QuoteAuthor'<\/span>]<span style=\"color: #666666\">=<\/span>quoteauthor\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'QuoteContent'<\/span>]<span style=\"color: #666666\">=<\/span>quotecontent\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'OriginalContent'<\/span>]<span style=\"color: #666666\">=<\/span>originalcontent\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'ThreadTitle'<\/span>]<span style=\"color: #666666\">=<\/span>threadtitle\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'ThreadAuthor'<\/span>]<span style=\"color: #666666\">=<\/span>threadauthor\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'ThreadID'<\/span>]<span style=\"color: #666666\">=<\/span>threadid\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'ThreadLink'<\/span>]<span style=\"color: #666666\">=<\/span>threadurl\r\n                    postdata[<span style=\"color: #008000\">int<\/span>(m<span style=\"color: #666666\">.<\/span>group())][<span style=\"color: #ba2121\">'PostLink'<\/span>]<span style=\"color: #666666\">=<\/span>postlink\r\n\r\n            postdatabig<span style=\"color: #666666\">.<\/span>update(postdata)\r\n\r\n            <span style=\"color: #008000;font-weight: bold\">try<\/span>:\r\n                nextpagetag<span style=\"color: #666666\">=<\/span>soup<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'a'<\/span>,attrs<span style=\"color: #666666\">=<\/span>{<span style=\"color: #ba2121\">'rel'<\/span>:<span style=\"color: #ba2121\">'next'<\/span>})\r\n                next_postpage<span style=\"color: #666666\">=<\/span>nextpagetag[<span style=\"color: #666666\">0<\/span>][<span style=\"color: #ba2121\">'href'<\/span>]\r\n            <span style=\"color: #008000;font-weight: bold\">except<\/span> <span style=\"color: #d2413a;font-weight: bold\">IndexError<\/span>:\r\n                <span style=\"color: #008000;font-weight: bold\">break<\/span>\r\n            <span style=\"color: #008000;font-weight: bold\">else<\/span>:\r\n                next_postpage<span style=\"color: #666666\">=<\/span>next_postpage<span style=\"color: #666666\">.<\/span>split(<span style=\"color: #ba2121\">'?'<\/span>)[<span style=\"color: #666666\">0<\/span>]\r\n\r\n    <span style=\"color: #008000;font-weight: bold\">try<\/span>:\r\n        nextthreadpagetag<span style=\"color: #666666\">=<\/span>soup2<span style=\"color: #666666\">.<\/span>find_all(<span style=\"color: #ba2121\">'a'<\/span>,attrs<span style=\"color: #666666\">=<\/span>{<span style=\"color: #ba2121\">'rel'<\/span>:<span style=\"color: #ba2121\">'next'<\/span>})\r\n        next_threadpage<span style=\"color: #666666\">=<\/span>nextthreadpagetag[<span style=\"color: #666666\">0<\/span>][<span style=\"color: #ba2121\">'href'<\/span>]\r\n    <span style=\"color: #008000;font-weight: bold\">except<\/span> <span style=\"color: #d2413a;font-weight: bold\">IndexError<\/span>:\r\n        <span style=\"color: #008000;font-weight: bold\">break<\/span>\r\n    <span style=\"color: #008000;font-weight: bold\">else<\/span>:\r\n        next_threadpage<span style=\"color: #666666\">=<\/span>next_threadpage<span style=\"color: #666666\">.<\/span>split(<span style=\"color: #ba2121\">'?'<\/span>)[<span style=\"color: #666666\">0<\/span>]\r\n\r\n<span style=\"color: #008000;font-weight: bold\">import<\/span> <span style=\"color: #0000ff;font-weight: bold\">os<\/span><span style=\"color: #666666\">,<\/span> <span style=\"color: #0000ff;font-weight: bold\">csv<\/span>\r\nos<span style=\"color: #666666\">.<\/span>chdir(<span style=\"color: #ba2121\">\"\/Users\/lulinghuang\/Desktop\/2016 DSC\/Learn Python\/\"<\/span>)\r\n\r\n<span style=\"color: #008000;font-weight: bold\">with<\/span> <span style=\"color: #008000\">open<\/span>(<span style=\"color: #ba2121\">\"scraping4TestOct23.csv\"<\/span>, <span style=\"color: #ba2121\">\"w\"<\/span>) <span style=\"color: #008000;font-weight: bold\">as<\/span> toWrite:\r\n    writer <span style=\"color: #666666\">=<\/span> csv<span style=\"color: #666666\">.<\/span>writer(toWrite, delimiter<span style=\"color: #666666\">=<\/span><span style=\"color: #ba2121\">\",\"<\/span>)\r\n    writer<span style=\"color: #666666\">.<\/span>writerow([<span style=\"color: #ba2121\">\"PostID\"<\/span>, <span style=\"color: #ba2121\">\"ThreadTitle\"<\/span>, \r\n    <span style=\"color: #ba2121\">'ThreadID'<\/span>,\r\n    <span style=\"color: #ba2121\">'ThreadAuthor'<\/span>,<span style=\"color: #ba2121\">'ThreadLink'<\/span>,<span style=\"color: #ba2121\">'PostLink'<\/span>,\r\n    <span style=\"color: #ba2121\">'PostPosition'<\/span>,<span style=\"color: #ba2121\">'PostDateAndTime'<\/span>,<span style=\"color: #ba2121\">'PostAuthor'<\/span>,<span style=\"color: #ba2121\">'JoinDate'<\/span>,\r\n    <span style=\"color: #ba2121\">'Location'<\/span>,<span style=\"color: #ba2121\">'LastSeen'<\/span>,<span style=\"color: #ba2121\">'Gender'<\/span>,<span style=\"color: #ba2121\">'IdeologicalLeaning'<\/span>,<span style=\"color: #ba2121\">'NumberOfPosts'<\/span>,\r\n    <span style=\"color: #ba2121\">'Addressee'<\/span>,<span style=\"color: #ba2121\">'QuoteAuthor'<\/span>,<span style=\"color: #ba2121\">'QuoteContent'<\/span>,<span style=\"color: #ba2121\">'OriginalContent'<\/span>])\r\n    \r\n    <span style=\"color: #008000;font-weight: bold\">for<\/span> a <span style=\"color: #aa22ff;font-weight: bold\">in<\/span> postdatabig<span style=\"color: #666666\">.<\/span>keys():\r\n        <span style=\"color: #008000;font-weight: bold\">try<\/span>:\r\n            writer<span style=\"color: #666666\">.<\/span>writerow([a,\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"ThreadTitle\"<\/span>]<span style=\"color: #666666\">.<\/span>encode(<span style=\"color: #ba2121\">'utf-8'<\/span>),\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"ThreadID\"<\/span>],\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"ThreadAuthor\"<\/span>]<span style=\"color: #666666\">.<\/span>encode(<span style=\"color: #ba2121\">'utf-8'<\/span>),\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"ThreadLink\"<\/span>]<span style=\"color: #666666\">.<\/span>encode(<span style=\"color: #ba2121\">'utf-8'<\/span>),\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"PostLink\"<\/span>]<span style=\"color: #666666\">.<\/span>encode(<span style=\"color: #ba2121\">'utf-8'<\/span>),\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"PostPosition\"<\/span>],\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"PostDateAndTime\"<\/span>]<span style=\"color: #666666\">.<\/span>encode(<span style=\"color: #ba2121\">'utf-8'<\/span>),\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"PostAuthor\"<\/span>]<span style=\"color: #666666\">.<\/span>encode(<span style=\"color: #ba2121\">'utf-8'<\/span>),\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"JoinDate\"<\/span>]<span style=\"color: #666666\">.<\/span>encode(<span style=\"color: #ba2121\">'utf-8'<\/span>),\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"Location\"<\/span>]<span style=\"color: #666666\">.<\/span>encode(<span style=\"color: #ba2121\">'utf-8'<\/span>),\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"LastSeen\"<\/span>]<span style=\"color: #666666\">.<\/span>encode(<span style=\"color: #ba2121\">'utf-8'<\/span>),\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"Gender\"<\/span>]<span style=\"color: #666666\">.<\/span>encode(<span style=\"color: #ba2121\">'utf-8'<\/span>),\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"IdeologicalLeaning\"<\/span>]<span style=\"color: #666666\">.<\/span>encode(<span style=\"color: #ba2121\">'utf-8'<\/span>),\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"NumberOfPosts\"<\/span>],\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"Addressee\"<\/span>]<span style=\"color: #666666\">.<\/span>encode(<span style=\"color: #ba2121\">'utf-8'<\/span>),\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"QuoteAuthor\"<\/span>]<span style=\"color: #666666\">.<\/span>encode(<span style=\"color: #ba2121\">'utf-8'<\/span>),\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"QuoteContent\"<\/span>]<span style=\"color: #666666\">.<\/span>encode(<span style=\"color: #ba2121\">'utf-8'<\/span>),\r\n            postdatabig[a][<span style=\"color: #ba2121\">\"OriginalContent\"<\/span>]<span style=\"color: #666666\">.<\/span>encode(<span style=\"color: #ba2121\">'utf-8'<\/span>)])\r\n        <span style=\"color: #008000;font-weight: bold\">except<\/span> <span style=\"color: #d2413a;font-weight: bold\">AttributeError<\/span> <span style=\"color: #008000;font-weight: bold\">as<\/span> detail:\r\n            <span style=\"color: #008000;font-weight: bold\">print<\/span> a, postdatabig[a][<span style=\"color: #ba2121\">\"PostPosition\"<\/span>], postdatabig[a][<span style=\"color: #ba2121\">\"PostLink\"<\/span>]\r\n            <span style=\"color: #008000;font-weight: bold\">print<\/span> detail\r\n            <span style=\"color: #008000;font-weight: bold\">continue<\/span>\r\n      \r\n<span style=\"color: #008000;font-weight: bold\">print<\/span> <span style=\"color: #ba2121\">'Done writing file.'<\/span>\r\n<\/span><\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><strong>Extract thread and post ids:<\/strong><\/p>\n<p>With Web Scraper, it was difficult to extract thread and post ids. In python, it is much easier. The post ids can be found in the &#8220;id&#8221; attribute of<\/p>\n<ul>\n<li>tags in the form of &#8220;post_##########&#8221;. An example:<!-- HTML generated using hilite.me -->\n<div style=\"background: #ffffff;overflow: auto;width: auto;border: solid gray;border-width: .1em .1em .1em .8em;padding: .2em .6em\">\n<pre style=\"margin: 0;line-height: 125%\"><\/pre>\n<\/div>\n<\/li>\n<li><span style=\"color: #0000cc\">class=<\/span><span style=\"background-color: #fff0f0\">&#8220;postbitlegacy postbitim postcontainer old&#8221;<\/span> <span style=\"color: #0000cc\">id=<\/span><span style=\"background-color: #fff0f0\">&#8220;post_1065546074&#8221;<\/span><span style=\"color: #007700\">&gt;<\/span><\/li>\n<li>\n<div style=\"background: #ffffff;overflow: auto;width: auto;border: solid gray;border-width: .1em .1em .1em .8em;padding: .2em .6em\">\n<pre style=\"margin: 0;line-height: 125%\"><\/pre>\n<\/div>\n<p>To extract the ten-digit id, after locating the appropriate tags at line 137, I used\u00a0<span style=\"text-decoration: underline\"><a href=\"https:\/\/docs.python.org\/2\/library\/re.html#\">regular expression<\/a><\/span> to search for the pattern &#8220;##########&#8221; and extract the matched pattern at lines 138-139. In line 137, &#8220;(?Extract texts only under a parent tag:<\/p>\n<p>Within a post, I need to separate quoted content (if there is any) from original content. These two kinds of content is approximately structured like the html in this hypothetical example:<\/p>\n<div style=\"background: #ffffff;overflow: auto;width: auto;border: solid gray;border-width: .1em .1em .1em .8em;padding: .2em .6em\">\n<div>\n<p><span style=\"color: #0000cc\">class=<\/span><span style=\"background-color: #fff0f0\">&#8220;1&#8221;<\/span><span style=\"color: #007700\">&gt;<\/span><\/p>\n<div><span style=\"color: #0000cc\">class=<\/span><span style=\"background-color: #fff0f0\">&#8220;2&#8221;<\/span><span style=\"color: #007700\">&gt;<\/span> Quoted content (easy to locate and extract)<\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<pre style=\"margin: 0;line-height: 125%\">    Original content (tricky to <span style=\"color: #007700\">&lt;b&gt;<\/span>isolate<span style=\"color: #007700\">&lt;\/b&gt;<\/span>)\r\n<span style=\"color: #007700\">&lt;\/div&gt;<\/span>\r\n<\/pre>\n<\/div>\n<p>It seems difficult to only select those texts outside the child tag.\u00a0My first solution was to replace all children tags with &#8220;&#8221; (i.e., an empty string). There are two problems with this solution: (1) all text formatting tags would be removed. In the above example, the word &#8220;isolate&#8221; would be missed by the scraper; (2) sometimes a user would post links directly or post words with hyperlinks, which means there would be &lt;a&gt; tags in the original content. The texts enclosed in these &lt;a&gt; tags would be useful. I would lose this information with the first solution.<\/p>\n<p>My current solution is to use Beautiful Soup&#8217;s &#8220;div.decompose()&#8221;\u00a0to remove only the &lt;div&gt; tags (lines 122-125). It works because I&#8217;ve known that the quoted content must be within some &lt;div&gt; tags and the original content must be outside all &lt;div&gt; tags. With this solution, I can preserve as much information as possible.<\/p>\n<p><strong>Respect the scraping courtesies:<\/strong><\/p>\n<p>In essence, be nice to the server. First, I included &#8220;time.sleep()&#8221; at lines 13 and 23, which told the scraper to wait and rest after each request. Second, I ran the scraper during off-peak hours so that the scraper did not disturb too much other users&#8217; access to the forum. Third, I identified myself to the server by including a header (lines 7, 11, and 21). <a href=\"http:\/\/robertorocha.info\/on-the-ethics-of-web-scraping\/\"><span style=\"text-decoration: underline\">You can find more information on scraping courtesies in Roberto Rocha&#8217;s (2015) article<\/span>.<\/a><\/p>\n<hr \/>\n<p>My next goal is to explore <em>pandas<\/em>, a python library for data cleaning and analysis. I had some troubles in converting a nested dictionary to <em>DataFrame<\/em> in pandas directly. I may have to write the dictionary to csv, and in a different python script, open and read the csv, then create a DataFrame from the csv. I&#8217;ll talk about it in the next post.<\/p>\n<p style=\"text-align: center\">References<\/p>\n<p style=\"text-align: left\">Rocha, R. (2015, March 8). On the ethics of web scraping [Blog article]. Retrieved from\u00a0http:\/\/robertorocha.info\/on-the-ethics-of-web-scraping\/<\/p>\n<p style=\"text-align: left\">Siotto, A. (2016, September 1).\u00a0Web scraping with Python [Blog article].\u00a0Retrieved from\u00a0https:\/\/sites.temple.edu\/tudsc\/2016\/09\/01\/web-scraping-with-python\/<\/p>\n<p style=\"text-align: left\">Zlotnick, F. (2014).\u00a0Hierarchical and associative data structures in python vs R [Web tutorial]. Retrieved from http:\/\/web.stanford.edu\/~zlotnick\/TextAsData\/More_on_Dictionaries.html<\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>By Luling Huang<\/p>\n","protected":false},"author":3163,"featured_media":2448,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[303,2,290],"tags":[64,71,169],"class_list":["post-2350","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-science","category-grad-students","category-political-science","tag-coding","tag-python","tag-web-scraping"],"_links":{"self":[{"href":"https:\/\/sites.temple.edu\/tudsc\/wp-json\/wp\/v2\/posts\/2350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sites.temple.edu\/tudsc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sites.temple.edu\/tudsc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sites.temple.edu\/tudsc\/wp-json\/wp\/v2\/users\/3163"}],"replies":[{"embeddable":true,"href":"https:\/\/sites.temple.edu\/tudsc\/wp-json\/wp\/v2\/comments?post=2350"}],"version-history":[{"count":0,"href":"https:\/\/sites.temple.edu\/tudsc\/wp-json\/wp\/v2\/posts\/2350\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sites.temple.edu\/tudsc\/wp-json\/wp\/v2\/media\/2448"}],"wp:attachment":[{"href":"https:\/\/sites.temple.edu\/tudsc\/wp-json\/wp\/v2\/media?parent=2350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sites.temple.edu\/tudsc\/wp-json\/wp\/v2\/categories?post=2350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sites.temple.edu\/tudsc\/wp-json\/wp\/v2\/tags?post=2350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}